﻿CREATE PROCEDURE [dbo].[usp_update_AlertSubscriptionByID]
(
     @AlertSubscriptionID uniqueidentifier
	,@Active bit
	,@Subscriber varchar(255)
	,@IsGroup bit
	,@CustomEmail varchar(255)
	,@UseStartAndEndTime bit
	,@StartUTCHour tinyint
	,@StartUTCMinute tinyint
	,@EndUTCHour tinyint
	,@EndUTCMinute tinyint
	,@BlackOutStartDate datetime
	,@BlackOutEndDate datetime
	,@LastFired datetime
	,@IntervalMinutes smallint
	,@ModifiedBy varchar(50)
	,@Debug					   bit			   = 0
)
AS
/****************************************************************************
Name: usp_update_AlertSubscriptionByID.sql
Description:
*****************************************************************************/
SET NOCOUNT ON;

-- Bootstrap
DECLARE @DebugPrefix       varchar(10);
DECLARE @ErrorCode         int;

SET @ErrorCode          = 0;
SET @DebugPrefix        = '>>> DEBUG: ';


-- Test harness
/*
-- declare the stored procedure paramters

DECLARE @Name;
DECLARE @Value;

-- now set them
SELECT
	 @Name  = 'value_1'
	,@Value  = 'value_2'
    ,@Debug                 = 1;

*/

-- Print parameters
IF (@Debug > 0)
BEGIN
    PRINT @DebugPrefix + '@AlertSubscriptionID = ' + CAST(@AlertSubscriptionID AS varchar);
END;

UPDATE [dbo].[AlertSubscription]
   SET 
      [Active] = @Active
      ,[Subscriber] = @Subscriber
      ,[IsGroup] = @IsGroup
      ,[CustomEmail] = @CustomEmail
	  ,[UseStartAndEndTime] = @UseStartAndEndTime
      ,[StartUTCHour] = @StartUTCHour
      ,[StartUTCMinute] = @StartUTCMinute
      ,[EndUTCHour] = @EndUTCHour
      ,[EndUTCMinute] = @EndUTCMinute
      ,[BlackOutStartDate] = @BlackOutStartDate
      ,[BlackOutEndDate] = @BlackOutEndDate
      ,[LastFired] = @LastFired
      ,[IntervalMinutes] = @IntervalMinutes
      ,[ModifiedDate] = GetUTCDate()
      ,[ModifiedBy] = @ModifiedBy
 WHERE
	[AlertSubscriptionID] = @AlertSubscriptionID


-- CHECK FOR ERROR
SET @ErrorCode = @@Error;
IF (@ErrorCode <> 0) GOTO ERROR_HANDLER;
			
RETURN 0;

-- Handles errors.
ERROR_HANDLER:
IF (@ErrorCode <> 0)
BEGIN
    IF (@Debug > 0) 
		BEGIN
			PRINT @DebugPrefix + 'Error Code = ' + CAST(@ErrorCode AS varchar(10)) + ' occurred.';
			RETURN @ErrorCode
		END

    -- Don't return any evidence of what specific error occurred, i.e. 
    -- we want to ward off potential hackers.
    RETURN 1;
END;

SET NOCOUNT OFF;
